package com.zhuiYun.project.api.seckill.controller;

import com.alibaba.fastjson.JSON;
import com.zhuiYun.project.annotation.AccessLimit;
import com.zhuiYun.project.api.order.entity.CommodityArr;
import com.zhuiYun.project.api.order.entity.SubmitOrderVo;
import com.zhuiYun.project.api.seckill.entity.SeckillDto;
import com.zhuiYun.project.api.seckill.service.SeckillService;
import com.zhuiYun.project.common.controller.BaseController;
import com.zhuiYun.project.common.errorCode.CommonException;
import com.zhuiYun.project.common.response.CommonConstants;
import com.zhuiYun.project.common.response.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName SeckillServiceImpl
 * @Description TODO
 * @Author lx
 * @Date 2023/4/3 15:00
 **/
@RestController
@RequestMapping("/seckill")
@Api(tags = "日志操作模块")
@CrossOrigin
@Slf4j
public class SeckillController extends BaseController {

    @Autowired
    SeckillService seckillService;
    @Resource
    private RedisTemplate redisTemplate;

    /**
    * @Description 将秒杀商品结束的时间存入redis
    * @Author lzy
    * @Date 2023/4/16 18:56
    * @Return void
    */
    @GetMapping("/seckillOverTime")
    public void seckillOverTime(){
        seckillService.seckillOverTime();
    }

    /**
    * @Author lx
    * @Description 首页展示5个限时抢购的商品
    * @Date 2023/4/3 19:26
    * @Return com.zhuiYun.project.common.response.CommonResult
    */
    @GetMapping("/getSeckillAll")
    @ApiOperation(value = "首页秒杀信息查询")
    public CommonResult getSeckillAll() {
        return CommonResult.ok(seckillService.getSeckillAll());
    }

    /**
     * @Author lx
     * @Description 限时抢购商品信息
     * @Date 2023/4/3 19:26
     * @Return com.zhuiYun.project.common.response.CommonResult
     */
    @GetMapping("/getSeckillList")
    @ApiOperation(value = "限时抢购商品信息")
    public CommonResult getSeckillList() {
        return CommonResult.ok(seckillService.getSeckillList());
    }


    /**
     * @Author lx
     * @Description 显示抢购
     * @Date 2023/4/5 15:36
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param userName 用户名
     * @param commId 商品id
     */
    @PostMapping("doSeckill")
    public CommonResult doSeckill(String userName, Integer commId) throws Exception{
        return CommonResult.ok(seckillService.doSeckill(userName,commId));
    }


    /**
    * @Description 使用redis分布式锁 提交订单
    * @Author lzy
    * @Date 2023/4/11 16:13
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param submitOrderVo
    */
    @PostMapping("/submitOrder")
    @ApiOperation(value = "redis+消息队列进行秒杀实现", notes = "redis+消息队列进行秒杀实现")
//    @LimitNumber(value = 2)//用于限制用户购买数量，value代表数量
    @AccessLimit(seconds = 1,maxCount = 800,ipSeconds =1,ipMaxCount = 20)//自定义注解，用于限流，限定指定时间范围内的访问量
    public CommonResult submitOrder(@RequestBody SubmitOrderVo submitOrderVo) throws CommonException {
        return seckillService.submitCommodityOrder(submitOrderVo);
    }

    /**
     * @Description RabbitMQ消息发送确认
     * @Author HKY
     * @Date 2023/4/11 16:13
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param submitOrderVo
     * 返回0表示正在消费生成订单中，前端继续轮询迭代
     */
    @PostMapping("/sendResult")
    public CommonResult sendResult(@RequestBody SubmitOrderVo submitOrderVo) throws CommonException {
        List<CommodityArr> commodityArrs = JSON.parseArray(submitOrderVo.getProductArr(), CommodityArr.class);
        StringBuffer extApiKey = new StringBuffer();
        extApiKey.append(CommonConstants.COMMODITY_SECKILL_SUCCESS+":"+submitOrderVo.getUserId()+":");
        for (CommodityArr commodityArr : commodityArrs) {
            extApiKey.append(commodityArr.getId());
        }
        Boolean aBoolean = redisTemplate.hasKey(String.valueOf(extApiKey));
        //查到秒杀商品成功标识直接返回结果集
        if (redisTemplate.hasKey(String.valueOf(extApiKey))){
            Object o = redisTemplate.opsForValue().get(String.valueOf(extApiKey));
            redisTemplate.delete(String.valueOf(extApiKey));
            return (CommonResult) o;
        }
        return CommonResult.ok(0);
    }
    /**
    * @Author lx
    * @Description 根据限时抢购商品点击查询限时抢购商品详细信息
    * @Date 2023/4/11 15:33
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param seckillDto
    */
    @GetMapping("/getSeckillMain")
    @ApiOperation(value = "商品详情方法")
    public CommonResult getSeckillMain(SeckillDto seckillDto){
        return CommonResult.ok(seckillService.getSeckillMain(seckillDto));
    }
}
